package demo;
//解决死锁（规定线程用锁的顺序）
public class Test5 {
        public static void main(String[] args) throws InterruptedException {
            Object locker1 = new Object();
            Object locker2 = new Object();
            Thread thread1 = new Thread(()->{
                synchronized (locker1){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (locker2){
                        System.out.println(1);
                    }
                }
            });
            Thread thread2 = new Thread(()->{
                synchronized (locker1){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (locker2){
                        System.out.println(2);
                    }
                }
            });
            thread1.start();
            thread2.start();
            thread1.join();
            thread2.join();
        }
}
